<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div class="goods_list">
          <van-cell @click="goDetail(item.id)" v-for="item in goodList" :key="item.id">
            <van-image fit="cover" :src="item.img_url"></van-image>
            <p>{{ item.title }}</p>
            <div class="bottom_box">
              <div>
                <span>￥{{item.sell_price}}</span>
                <s>￥2195</s>
              </div>
              <div>
                <span>热卖中</span>
                <span>剩{{item.stock_quantity}}件</span>
              </div>
            </div>
          </van-cell>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false,
      goodList: [],
      loading: false,
      finished: false,
    };
  },
  mounted() {
    this.getgoodList();
  },
  methods: {
    async getgoodList() {
      const { data: res } = await this.$http.get(`/api/getgoods?pageindex=1`);
      this.goodList = res.message;
    },
    goDetail(id) {
      this.$router.push("/goods/detail/" + id);
    },
    onLoad() {
      setTimeout(async () => {
        const { data: res } = await this.$http.get(`/api/getgoods?pageindex=2`);
        this.goodList.push(...res.message);
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.goodList.length >= 10) {
          this.finished = true;
        }
      }, 500);
    },
    onRefresh() {
      setTimeout(() => {
        this.$toast("刷新成功");
        this.isLoading = false;
      }, 500);
    }
  }
};
</script>

<style lang="less" scoped>
.goods_list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 5px 10px;

  .van-cell {
    width: 48%;
    border: 1px solid #ccc;
    margin: 10px 0px;
    padding: 0px;
    align-content: space-between;

    .van-cell__value--alone {
      text-align: center;
      .van-image {
        padding: 10px;
        min-height: 180px;
      }
    }

    img {
      min-height: 180px;
      height: 180px;
      margin: auto;
    }

    .van-cell__value {
      > p {
        font-size: 14px;
        color: #000;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        line-clamp: 2;
        padding: 4px 10px;
        height: 49px;
      }
    }
    .bottom_box {
      width: 100%;
      background: #eee;
      padding: 5px 0px;
      div:nth-child(1) {
        text-align: left;
        padding: 0px 10px;
        span:nth-child(1) {
          font-size: 16px;
          color: red;
          margin-right: 15px;
        }
      }
      div:nth-child(2) {
        display: flex;
        justify-content: space-between;
        padding: 0px 10px;
      }
    }
  }
}
</style>